# @Time:   2021/2/2 17:15
# @Author: Zong Wei

import random
from time import sleep

from faker import Faker
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait

from ui_automation_testing.test.pageObject.loginPage import LoginPage
from ui_automation_testing.utils.logger import Logger


class FeeProjectPage(LoginPage):
    logger = Logger(__name__).get_log()
    more_button = (By.XPATH, "(//span[@class='caret'])[1]")
    base_menu = (By.XPATH, "//ul[@class='dropdown-menu']//span[.='基础资料']")
    menu = (By.XPATH, "//ul[@class='site-menu']//span[.='财务相关']")
    fee_project_menu = (By.XPATH, "(//li[@class='site-menu-item']//span[.='费用项目'])[2]")
    search_button = (By.ID, "lr-search")
    add_button = (By.ID, "lr-add")
    edit_button = (By.ID, "lr-edit")
    del_button = (By.ID, "lr-delete")
    export_button = (By.ID, "lr-export")
    export_excel = (By.CSS_SELECTOR, ".layui-layer-btn0")

    fee_num = (By.ID, "FeeTypeNo")
    cn_name = (By.ID, "FeeTypeNameC")
    en_name = (By.ID, "FeeTypeNameE")
    remember_code = (By.ID, "RemberCode")
    creater = (By.ID, "CreaterID")
    service_item = (By.ID, "FeeTypeClassNameC")
    is_active = (By.ID, "IsActive")
    biz_class = (By.XPATH, "//div[@id='jobtype']//input")
    biz_part = (By.ID, "Tabbasic")
    part_value = (By.XPATH, "(//div[@id='basic']//input)[position()>1]")
    all_fee = (By.XPATH, "//div[@type='select']")
    alias_part = (By.ID, "Tabalias")
    add_alias = (By.CSS_SELECTOR, "#lr-Import-alias")
    alias_name = (By.XPATH, "(//*[@id='datagrid-row-r1-2-0']//input)[2]")
    alias_en = (By.XPATH, "(//*[@id='datagrid-row-r1-2-0']//input)[3]")
    del_alias = (By.CSS_SELECTOR, "#lr-delete-alias")
    confirm_button = (By.XPATH, "//a[.='确认']")
    first_row = (By.XPATH, "//span[.='费用编号']//following::tr[1]")

    def __init__(self):
        self.driver = LoginPage().login()
        self.wait = WebDriverWait(self.driver, 10)

    def fee_project_home(self):
        self.logger.info("登录系统成功")
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.more_button)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.base_menu)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.menu)).click()
        self.wait.until(ec.element_to_be_clickable(self.fee_project_menu)).click()
        sleep(1)
        return self.driver

    def add_fee(self, num='12345', cn='测试费用', en='Test Fee', code='ts', item='测试项目'):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.element_to_be_clickable(self.add_button)).click()
        self.logger.info("添加费用项目")
        sleep(2)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(3)
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.fee_num)).send_keys(num)
        self.wait.until(ec.presence_of_element_located(self.cn_name)).send_keys(cn)
        self.wait.until(ec.presence_of_element_located(self.en_name)).send_keys(en)
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.remember_code)).send_keys(code)
        self.wait.until(ec.presence_of_element_located(self.service_item)).send_keys(item)
        self.wait.until(ec.presence_of_element_located(self.is_active)).click()
        sleep(1)
        val1 = random.sample(self.driver.find_elements(*self.biz_class), 3)
        for i in val1:
            i.click()
            sleep(0.5)
        self.wait.until(ec.presence_of_element_located(self.biz_part)).click()
        self.logger.info("切换至‘业务板块’Tab")

        val2 = random.sample(self.driver.find_elements(*self.part_value), 3)
        for j in val2:
            j.click()
            sleep(0.5)

        val3 = self.driver.find_elements(*self.all_fee)
        functions = ['FF', 'WS', 'TP', 'TD', 'FN', 'XZ']
        seq = 0
        for k in val3:
            k.click()
            sleep(1)
            fee_values = "//div[@id='ChargeClass_" + functions[seq] + "-option']//li[position()>1]"
            random.choice(self.driver.find_elements_by_xpath(fee_values)).click()
            seq += 1
            sleep(1)

        self.wait.until(ec.presence_of_element_located(self.alias_part)).click()
        self.logger.info("切换至‘费用别名’Tab")
        self.wait.until(ec.element_to_be_clickable(self.add_alias)).click()
        self.logger.info("添加费用别名")
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.alias_name)).send_keys('测试别名')
        self.wait.until(ec.presence_of_element_located(self.alias_en)).send_keys('test alias')
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.del_alias)).click()
        self.logger.info("删除费用别名")
        sleep(1)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        sleep(1)
        self.logger.info("新增费用项目成功")

    def search_fee(self):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.presence_of_element_located(self.remember_code)).send_keys('ts')
        self.wait.until(ec.presence_of_element_located(self.cn_name)).send_keys('测试费用')
        self.wait.until(ec.presence_of_element_located(self.en_name)).send_keys('Test Fee')
        # self.wait.until(ec.presence_of_element_located(self.creater)).send_keys('U10725')
        self.wait.until(ec.element_to_be_clickable(self.search_button)).click()
        sleep(2)
        self.logger.info("查询完成")

    def edit_fee(self):
        self.search_fee()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.edit_button)).click()
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(3)
        self.wait.until(ec.presence_of_element_located(self.en_name)).clear()
        self.wait.until(ec.presence_of_element_located(self.en_name)).send_keys(Faker().job())
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.service_item)).clear()
        self.wait.until(ec.presence_of_element_located(self.service_item)).send_keys(Faker().company())
        sleep(2)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        self.logger.info("编辑修改完成")

    def del_fee(self):
        self.search_fee()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.del_button)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        self.logger.info("删除费用项目成功")

    def export_fee(self):
        self.search_fee()
        self.wait.until(ec.element_to_be_clickable(self.export_button)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.export_excel)).click()
        sleep(2)
        self.logger.info("导出成功")


if __name__ == '__main__':
    fp = FeeProjectPage()
    fp.fee_project_home()
    fp.export_fee()
